<?php
namespace app\agentapi\controller\user;
use app\agentapi\controller\Controller;
use think\Cache;
use think\Db;
use Predis\Client;
/**
 * 楼盘
 */
class Build extends Controller{
    public $server;
    public $redis;
    public function __construct(){
        $this->server=[
            'host' => '127.0.0.1',
            'port' => 6379,
        ];
        $this->redis= new Client($this->server);
    }
    /**
     * 获取楼盘列表
     */
    public function getBuildList(){
        //销售管理员，置业顾问获取所属区域楼盘列表、楼管人员获取所有楼盘
        //1：主管理员 2：城市管理员 3：区域管理员 4:销售管理员  5：置业顾问 6：楼管人员
        //$userInfo=Db::name('love_admin_user')->where(['id'=>$this->userId])->find();
        /*if($userInfo['type']==4||$userInfo['type']==5){
            //4:销售管理员  5：置业顾问  获取所属区域楼盘
            $where['areaId']=['=',$userInfo['areaid']];
        }else{
            //楼管人员  //获取所有楼盘
            $where['id']=['>',0];
        }*/
        $where['id']=['>',0];
        $where['status']=['=',1];
        //搜索
        $buildname=input('post.buildname');
        $type=input('type');//1:新房 2：二手  3 文旅
        if($type){
            $where['type']=['=',$type];
        }
        if($buildname){
            $where['name']=['like','%'.$buildname.'%'];
        }
        $list=Db::name('love_building')->where($where)->order('zhiding desc,id desc')->select()->toArray();
        if($list){
            foreach ($list as $k=>$v){
                $ids=json_decode($v['loupanimageids'],'true');
                $imageInfo=Db::name('upload_file')->where(['file_id'=>$ids['0']])->field('file_id,file_url,file_name')->find();
                $list[$k]['imageurl']=$imageInfo['file_url'].'/'.$imageInfo['file_name'];
            }
        }
        return  json(['msg'=>'获取成功','code'=>'success','data'=>$list]);
    }
    /**
     * 获取楼盘详情
     */
    public function getBuildInfo(){
        $buildId=intval(input('post.buildid'));
        $userid=intval(input('post.fenxiangId'));
        if(empty($buildId)){
            return json(['msg'=>'楼盘id不能为空','code'=>'error','data'=>[]]);
        }//判断该房源状态
        $buildInfo=Db::name('love_building')->where(['id'=>$buildId])->find();
        if(empty($buildInfo)){
            return json(['msg'=>'未获取到楼盘信息','code'=>'error','data'=>[]]);
        }
        if($buildInfo['status']==2){
            return json(['msg'=>'该楼盘已下架','code'=>'error','data'=>[]]);
        }
        $buildInfo['bannerids']=$this->getImageUrl(json_decode($buildInfo['bannerids'],'true'));
        $buildInfo['loupanimageids']=$this->getImageUrl(json_decode($buildInfo['loupanimageids'],'true'));
        $buildInfo['shapanimageids']=$this->getImageUrl(json_decode($buildInfo['shapanimageids'],'true'));
        $buildInfo['peitaoimageids']=$this->getImageUrl(json_decode($buildInfo['peitaoimageids'],'true'));
        $buildInfo['slimageids']=$this->getImageUrl(json_decode($buildInfo['slimageids'],'true'));
        $buildInfo['characteristic']=explode('@',$buildInfo['characteristic']);
        if($userid){
            $userInfo=Db::name('love_admin_user')->where(['id'=>$userid])->find();
            $buildInfo['userInfo']['phone']=$userInfo['user_name'];
            $buildInfo['userInfo']['name']=$userInfo['name'];
            if($userInfo['imageid']){
                $file=db('upload_file')->where(['file_id'=>$userInfo['imageid']])->find();
                $buildInfo['userInfo']['imageUrl']=$file['file_url'].'/'.$file['file_name'];
            }else{
                $buildInfo['userInfo']['imageUrl']='';
            }
        }
        //获取城市  区域信息
        $c=Db::name('love_area')->where(['id'=>$buildInfo['areaId']])->value('name');
        $b=Db::name('love_area')->where(['id'=>$buildInfo['cityId']])->value('name');
        $buildInfo['cityname']=$b;
        $buildInfo['areaname']=$c;
        return json(['msg'=>'获取成功','code'=>'success','data'=>$buildInfo]);
    }
    /**
     * 获取图片链接
     */
    private function getImageUrl($ids){
        $imageInfos=Db::name('upload_file')->where('file_id','in',$ids)->select();
        $arr=[];
        if($imageInfos){
            foreach($imageInfos as $k=>$v){
                $arr[]=$v['file_url'].'/'.$v['file_name'];
            }
        }
        return $arr;
    }


    /**
     * 测试   写入队列
     */
    public function addredis(){
        $data=input('post.data');
        $res=$this->redis->rpush('customerData1',$data);
        if($res){
            return json(['msg'=>'写入成功','code'=>'success','data'=>$res]);
        }
    }

    /**
     * 测试   获取队列
     */
    public function getredis(){
        $res=$this->redis->lrange('customerData1',0,-1);
        if($res){
            return json(['msg'=>'获取成功','code'=>'success','data'=>$res]);
        }
    }

    /**
     * 测试   顺序获取
     */
    public function getredisor(){
        $res=$this->redis->lpop('customerData1');
        if($res){
            return json(['msg'=>'获取成功','code'=>'success','data'=>$res]);
        }
    }

    /**
     * 发布消息
     */
    public function sub(){
        $order = [
            'id' => 1,
            'name' => '小米6',
            'price' => 2499,
            'created_at' => '2017-07-14'
        ];
        $this->redis->publish("order", json_encode($order));
    }
}
